"""
@author: xxw
@file: 08.协程.py
@time: 2023/7/3 14:38
"""

# 协程是一种用户态的轻量级线程，协程的调度完全由用户控制

# https://docs.python.org/zh-cn/3.9/library/asyncio-task.html

# 高并发网络模型  协程+epoll
import asyncio
import time

async def say_after(delay, what):
    await asyncio.sleep(delay)
    print(what)

async def main():
    task1 = asyncio.create_task(
        say_after(1, 'hello'))

    task2 = asyncio.create_task(
        say_after(2, 'world'))

    print(f"started at {time.strftime('%X')}")

    # Wait until both tasks are completed (should take
    # around 2 seconds.)
    await task1
    await task2

    print(f"finished at {time.strftime('%X')}")
asyncio.run(main())